<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>管理系统</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  {:widget_st('layui','css')}
  {:widget_st('admin','css')}
</head>
{:do_action('admin_head')}
<body>

<div class="layui-fluid">
  <div class="layui-card">
    <div>
      <?php if($type!=''){ ?>
        <span class="layui-badge layui-bg-red">类型:<?php echo $type; ?></span>
      <?php } ?>
        <span class="layui-badge layui-bg-orange">pid:{$pid}</span>


    </div>

    <form onclick="return false;" class="info_tj">
      <div class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">

          <div class="layui-inline">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
              <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">栏目tpl</label>
            <div class="layui-input-inline">
              <input type="text" name="tpl_category" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">内容tpl</label>
            <div class="layui-input-inline">
              <input type="text" name="tpl_post" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">模型</label>
            <div class="layui-input-inline">
              <select name="mid">
                <?php foreach($mlist as $k=>$vo):?>
                <option value="{$vo['id']}">{$vo['name']}</option>
                <?php endforeach;  ?>
              </select>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">父类</label>
            <div class="layui-input-inline">
              <select name="pid">
                <?php if($pid==0){ ?>
                <option value="0">---顶级目录---</option>
                <?php }else{ ?>
                <option value="{$pid}"><?php echo get_cate_name('category',$pid); ?></option>
                <?php } ?>
                <?php if(is_array($list)){ ?>
                <?php foreach($list as $k=>$vo){ ?>
                <option value="{$vo['cid']}"><?php echo '┃'.str_repeat('━━', substr_count($vo['cname'],'  '));?> {$vo['name']}</option>
                <?php  }} ?>
              </select>
            </div>
          </div>
          <input name="type" value="{$type}" type="hidden" />
          <input name="menu" value="1" type="hidden" />
          <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" onclick="tijiao_data_now('{:url("admin/category/category_add")}',0)"  >新增</button>
          </div>
        </div>
      </div>
    </form>


    <div class="layui-card-body">
      <?php if($pid!=0){?>
        <button class="layui-btn layui-btn-sm " onclick='zfAdminShow("父类栏目","{:url('admin/category/category_edit',['cid' => $pid] )}")'>父类栏目</button>
      <?php }?>
      <button class="layui-btn layui-btn-sm" onclick='zfAdminShow("批量修改栏目","{:url('admin/category/cate_all_edit',['type'=>$type])}")'>批量修改栏目</button>
      <button class="layui-btn layui-btn-sm" id="expandAll">一键展开</button>
      <button class="layui-btn layui-btn-sm" id="collapseAll">一键收缩</button>
      <table class="layui-table">
        <colgroup>
          <col width="50">
          <col>
        </colgroup>
        <thead>
        <tr>
          <th>CID</th>
          <th>名称</th>
          <th>栏目Tpl</th>
          <th>内容Tpl</th>
          <th>模型</th>
          <th>排序</th>
          <th>状态</th>
          <th>菜单</th>
          <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <?php foreach($list as $k=>$vo){ ?>
        <tr data-id="{$vo['cid']}" data-pid="<?php echo substr_count($vo['cname'],'  ') > 0 ? $vo['pid'] : '0'; ?>" class="<?php echo substr_count($vo['cname'],'  ') > 0 ? 'child-row' : ''; ?>">
          <td>{$vo['cid']}</td>
          <td>
            <?php
                    $indent = substr_count($vo['cname'],'  ');
                    echo str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $indent);
                    if($indent < substr_count($list[$k+1]['cname'] ?? '', '  ')):
                    ?>
            <span class="expand-btn" data-id="{$vo['cid']}">➕</span>
            <?php else: ?>
            <span class="no-expand">&nbsp;&nbsp;</span>
            <?php endif; ?>
            <?php if(ZFC("webconfig.isshow_form_parm")==1){ ?>
              <a onclick='zfAdminShow("编辑参数","{:url('admin/setting/category_form_parm',['id' => $vo['cid']] )}")'>
            <?php }else{ ?>
              <a href="#">
            <?php } ?>
            {$vo['name']}
            </a>
            <?php if(isset($vo['form_parm']) && $vo['form_parm']!=''){ echo ''; } ?>
          </td>
          <td>{$vo['tpl_category']}</td>
          <td>{$vo['tpl_post']}</td>
          <td><?php echo m_name($vo['mid'],'category_model'); ?></td>
          <td>
            <input type="text" name="sort" autocomplete="off" class="layui-input edit_sort"  value="{$vo['sort']}" item-id="{$vo['cid']}" item-dbname="category" >
          </td>
          <td>
            <div class=" layui-form" lay-filter="component-form-element">
              <input type="checkbox" name="status" <?php echo $vo['status']==1?'checked':''; ?> lay-skin="switch" lay-text="开启|关闭" lay-filter="status_change" item="{$vo['cid']}">
            </div>
          </td>
          <td>
            <div class=" layui-form" lay-filter="component-form-element">
              <input type="checkbox" name="menu" <?php echo $vo['menu']==1?'checked':''; ?> lay-skin="switch" lay-text="开启|关闭" lay-filter="menu_change" item="{$vo['cid']}">
            </div>
          </td>
          <td>
            <a class="layui-btn btn_del layui-btn-danger layui-btn-xs" rel="{$vo['cid']}" href="#">删除</a> <br>
            <button class="layui-btn layui-btn-xs " onclick='zfAdminShow("编辑栏目","{:url('admin/category/category_edit',['cid' => $vo['cid']] )}")'>编辑</button> <br>
            <?php if(!if_pid($vo['cid'])):  if($vo['mid']!=1): ?>
            <a class="layui-btn layui-btn-xs" href="{:url('admin/category/post_list',['cid' => $vo['cid'],'mid'=>$vo['mid']] )}">内容列表</a>
            <?php endif; endif; ?>
          </td>
        </tr>
        <?php } ?>
        </tbody>
      </table>

    </div>
  </div>
</div>


{:widget_st('jq','js')}
{:widget_st('layui','js')}
{:widget_st('common','js')}
<script>
  layui.use([ 'table','element'], function(){
    var $ = layui.$
            ,form = layui.form
            ,element = layui.element
            ,table = layui.table;

    // 删除
    $(".btn_del").on("click",function(){
      var id = $(this).attr("rel");
      layer.confirm('确认删除？', {
        btn: ['删除','取消']
      }, function(){

        $.get("{:url('admin/common/del_post')}",{id:id,db:'category'},function(res){
          if(res.result==1){
            layer.msg("删除成功", {icon: 1});
            setTimeout(function() {
              location.reload(true);
            }, 1000);
          }else{
            layer.msg(res.msg, {icon: 2});
          }
        },"json");

      }, function(){
        //取消的操作
      });
    });
    form.on('switch(menu_change)', function(data){
      var id = $(this).attr("item")
      var dbname = 'category'
      var status = this.checked ? '1' : '0'
      console.log(id)
      $.get("{:url('admin/common/is_menu')}",{id:id,dbname:dbname,menu:status},function(res){
        if(res.result==1){
          layer.msg(res.msg, {icon: 1});
        }else{
          layer.msg(res.msg, {icon: 2});
        }
      },"json");
    });
    form.on('switch(status_change)', function(data){
      var id = $(this).attr("item")
      var dbname = 'category'
      var status = this.checked ? '1' : '0'
      console.log(id)
      $.get("{:url('admin/common/is_switch')}",{id:id,dbname:dbname,status:status},function(res){
        if(res.result==1){
          layer.msg(res.msg, {icon: 1});
        }else{
          layer.msg(res.msg, {icon: 2});
        }
      },"json");
    });








  });

  window.tijiao_data_now = function(role,type=1){
    var index = layer.load(2);
    var data = $(".info_tj input,.info_tj select,.info_tj textarea,.info_tj option,.info_tj radio").serialize();
    $.ajax({
      type:'post',
      url:role,
      data:data,
      dataType:'json',
      success:function(res){
        console.log(res)
        if(res.result==1){
          layer.msg(res.msg, {icon: 1});
          layer.close()
          setTimeout(function() {
            if(type==1){
              window.parent.location.reload();
            }else{
              window.location.reload();
            }
          }, 2000);
        }else{
          layer.msg(res.msg, {icon: 2});
          layer.close(index)
        }
      }
    })
  }

  // 折叠功能
  $('.expand-btn').on('click', function() {
    var id = $(this).data('id');
    var $this = $(this);
    var $childRows = $('tr[data-pid="' + id + '"]');

    if ($this.text() === '➕') {
      $this.text('➖');
      $childRows.show();
    } else {
      $this.text('➕');
      $childRows.hide();
      // 递归隐藏所有子级
      hideChildren($childRows);
    }
  });

  function hideChildren($rows) {
    $rows.each(function() {
      var id = $(this).data('id');
      var $childRows = $('tr[data-pid="' + id + '"]');
      $childRows.hide();
      $(this).find('.expand-btn').text('➕');
      hideChildren($childRows);
    });
  }

  // 初始化时隐藏所有子级
  $('.child-row').hide();

  // 一键展开
  $('#expandAll').on('click', function() {
    $('.child-row').show();
    $('.expand-btn').text('➖');
  });
  $('#expandAll').click();

  // 一键收缩
  $('#collapseAll').on('click', function() {
    $('.child-row').hide();
    $('.expand-btn').text('➕');
  });
</script>
</body>
</html>
{:do_action('admin_js')}

